package com.tengxing.e1.FactoryTest;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.maiarstar.e101.protocol.cerebellum.api.exception.ProtocolException;
import com.maiarstar.e101.protocol.cerebellum.api.model.LEDInfo;
import com.maiarstar.e101.protocol.cerebellum.api.module.ICerebellumProtocolGetter;
import com.maiarstar.e101.protocol.cerebellum.api.module.ICerebellumProtocolSetter;
import com.maiarstar.e101.protocol.cerebellum.sdk.CerebellumProtocolSdkManager;

public class LED_Test_Activity extends AppCompatActivity {

    private static final String TAG = "LED_Test_Activity";
    private CerebellumProtocolSdkManager cerebellumProtocolSdkManager;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Button btn_LED_Pass, btn_LED_Fail, btn_LED_Get;
    TextView tV_LED1_Set, tV_LED2_Set, tV_LED3_Set;
    SeekBar seekBar_LED1, seekBar_LED2, seekBar_LED3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_led_test);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        sharedPreferences = getSharedPreferences("Test_Result", MODE_PRIVATE);
        initCerebellumManager();
        tV_LED1_Set = findViewById(R.id.tV_LED1_Set);
        tV_LED2_Set = findViewById(R.id.tV_LED2_Set);
        tV_LED3_Set = findViewById(R.id.tV_LED3_Set);
        seekBar_LED1 = findViewById(R.id.seekBar_LED1);
        seekBar_LED2 = findViewById(R.id.seekBar_LED2);
        seekBar_LED3 = findViewById(R.id.seekBar_LED3);
        Factory_LED_SB(1, seekBar_LED1, tV_LED1_Set);
        Factory_LED_SB(2, seekBar_LED2, tV_LED2_Set);
        Factory_LED_SB(3, seekBar_LED3, tV_LED3_Set);
        Factory_LED_BTN();
    }

    private void initCerebellumManager() {
        CerebellumProtocolSdkManager sdkProtocolManager = new CerebellumProtocolSdkManager(this);
        CerebellumProtocolSdkManager.SdkParams sdkParams = CerebellumProtocolSdkManager.getDeafultSdkParams();
        try {
            sdkProtocolManager.init(sdkParams);
        } catch (ProtocolException e) {
            Log.w(TAG, e.getMessage());
        }
        this.cerebellumProtocolSdkManager = sdkProtocolManager;
    }
    private void Factory_LED_SB(int num, SeekBar seekBar, TextView textView){
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int cyc_time = 0;
            String textView_str = "LED"+num+"设定值：";
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                switch (progress){
                    case 0:{
                        cyc_time = 0;
                        break;
                    }
                    case 1:{
                        cyc_time = 200;
                        break;
                    }
                    case 2: {
                        cyc_time = 1000;
                        break;
                    }
                    case 3: {
                        cyc_time = 9999;
                        break;
                    }
                    default:
                        break;
                }
                textView_str = "LED"+num+"设定值："+cyc_time+"ms";
                textView.setText(textView_str);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(SetLED(num, cyc_time)){
                    Toast.makeText(LED_Test_Activity.this, textView_str+"设定成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LED_Test_Activity.this, textView_str+"设定失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void Factory_LED_BTN(){
        btn_LED_Pass = findViewById(R.id.btn_LED_Pass);
        btn_LED_Pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("LED","通过");
                if(editor.commit()){
                    Toast.makeText(LED_Test_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LED_Test_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }else{
                    Toast.makeText(LED_Test_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_LED_Fail = findViewById(R.id.btn_LED_Fail);
        btn_LED_Fail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("LED","未通过");
                if(editor.commit()){
                    Toast.makeText(LED_Test_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LED_Test_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }else{
                    Toast.makeText(LED_Test_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_LED_Get = findViewById(R.id.btn_LED_Get);
        btn_LED_Get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Refresh_LED_Status(btn_LED_Get)){
                    Toast.makeText(LED_Test_Activity.this, "刷新成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LED_Test_Activity.this, "刷新失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    private boolean Refresh_LED_Status(Button btn){
        String led_info_str = "刷新LED当前状态：";
        boolean ret = true;
        int cyc_time = -1;
        for(int i=1; i<4 ; i++){
            cyc_time = GetLED(i);
            if(cyc_time == -1){
                led_info_str = led_info_str.concat("LED"+i+"：获取失败  ");
                ret = false;
                Toast.makeText(LED_Test_Activity.this, "刷新成功", Toast.LENGTH_SHORT).show();
            }else{
                led_info_str = led_info_str.concat("LED"+i+"："+cyc_time+"ms  ");
            }
        }
        btn.setText(led_info_str);
        return ret;
    }

    private int GetLED(int num) throws ProtocolException {
        try {
            ICerebellumProtocolGetter getter = this.cerebellumProtocolSdkManager.getCerebellumProtocolGetter();
            return getter.getLEDInfo(num).getPower();
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return -1;
    }

    private boolean SetLED(int num, int cyc_time) throws ProtocolException {
        try {
            LEDInfo ledInfo = new LEDInfo();
            ledInfo.setOrder(num);
            ledInfo.setPower(cyc_time);
            ICerebellumProtocolSetter setter = this.cerebellumProtocolSdkManager.getCerebellumProtocolSetter();
            return setter.setLEDInfo(ledInfo);
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return false;
    }
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(LED_Test_Activity.this, MainActivity.class);
        startActivity(intent);
        finish();
        Toast.makeText(LED_Test_Activity.this, "直接退出不会保存结果", Toast.LENGTH_SHORT).show();
        super.onBackPressed();
    }
}